
import { InjectRepository } from '@nestjs/typeorm';
import { Repository ,Not ,In,Like} from 'typeorm';
import { Injectable } from '@nestjs/common';
import { ResultData } from 'src/common/utils/result';
import { CreateCampusDto, UpdateCampusDto, QueryCampusDto } from './dto/campus.dto';
import { CampusEntity } from './entities/campus.entity';

@Injectable()
export class CampusService {
constructor(
    @InjectRepository(CampusEntity)
    private readonly campusEntityRep: Repository<CampusEntity>,
) {}
    async create(createCampusDto: CreateCampusDto) {
        const res = await this.campusEntityRep.save(createCampusDto);
        return ResultData.ok(res);
    }

    async findAll(query :QueryCampusDto ) {
        const entity = this.campusEntityRep.createQueryBuilder('entity');
        entity.where({ delFlag: '0'});
        if(query.campusId){
          entity.andWhere("entity.campusId = :campusId", {campusId: query.campusId});
        }
        if(query.schoolId){
            entity.andWhere("entity.schoolId = :schoolId", {schoolId: query.schoolId});
        }
        if(query.status){
              entity.andWhere("entity.status = :status", {status: query.status});
            }
        if(query.createBy){
              entity.andWhere("entity.createBy = :createBy", {createBy: query.createBy});
            }
        if(query.createTime){
              entity.andWhere("entity.createTime BETWEEN :start AND :end", { start: query.params.beginTime, end: query.params.endTime });
            }
        if(query.updateBy){
              entity.andWhere("entity.updateBy = :updateBy", {updateBy: query.updateBy});
            }
        if(query.updateTime){
              entity.andWhere("entity.updateTime BETWEEN :start AND :end", { start: query.params.beginTime, end: query.params.endTime });
            }
        if(query.remark){
              entity.andWhere("entity.remark = :remark", {remark: query.remark});
            }
        if(query.campusCode){
              entity.andWhere("entity.campusCode = :campusCode", {campusCode: query.campusCode});
            }
        if(query.origin){
              entity.andWhere("entity.origin = :origin", {origin: query.origin});
            }
        if(query.position){
              entity.andWhere("entity.position = :position", {position: query.position});
            }
        if(query.address){
              entity.andWhere("entity.address = :address", {address: query.address});
            }
        if(query.contactName){
              entity.andWhere("entity.contactName LIKE :contactName", {contactName: `%${query.contactName}%`});
            }
        if(query.contactPhone){
              entity.andWhere("entity.contactPhone = :contactPhone", {contactPhone: query.contactPhone});
        }
        if (query.orderByColumn && query.isAsc) {
          const key = query.isAsc === 'ascending' ? 'ASC' : 'DESC';
          entity.orderBy(`entity.${query.orderByColumn}`, key);
        }
        if (query.pageSize && query.pageNum) {
            entity.skip(query.pageSize * (query.pageNum - 1)).take(query.pageSize);
        }
        const [list, total] = await entity.getManyAndCount();

        // 如果没有分页参数则返回所有数据
        if (!query.pageSize || !query.pageNum) {
            const list = await entity.getMany();
            return ResultData.ok(list);
        }

        return ResultData.ok({
            list,
            total,
        });
    }

    async findOne(id: number) {
        const res = await this.campusEntityRep.findOne({
            where: {
                delFlag: '0',
                campusId: id,
            },
        });
        return ResultData.ok(res);
    }

    async update(updateCampusDto: UpdateCampusDto) {
        const res = await this.campusEntityRep.update({  campusId: updateCampusDto.campusId }, updateCampusDto);
        return ResultData.ok({value:res.affected >= 1});
    }

    async remove(campusIds: number[]) {
        const res = await this.campusEntityRep.update(
            { campusId: In(campusIds) },
            {
                delFlag: '1',
            },
        );
        return ResultData.ok({value:res.affected >= 1});
    }
}
